﻿// Example header text. Can be configured in the options.
using System;
using System.Data;
using vtech_frameWorks.DAO;
using vtech_frameWorks.Exceptions;
using vtech_frameWorks.Object;

namespace vtech_system.User
{
    public class ClsUserBS : ClsObjectBS
    {
        #region "Insert"
        public override void DoInsert(DataTable dt)
        {
            ClsUserDAO obj = new ClsUserDAO();
            try
            {
                conn = ClsBaseDbConnectionDAO.GetConnection();
                conn.Open();
                tran = conn.BeginTransaction();

                foreach (DataRow drow in dt.Rows)
                {
                    obj.Insert(drow);
                }

                tran.Commit();
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                {
                    ClsInformation.Information(ex.Message);
                }
                if (tran != null)
                {
                    tran.Rollback();
                }
            }
            finally
            {
                if (conn != null && conn.State != ConnectionState.Closed)
                {
                    conn.Close();
                }
            }
        }

        #endregion

        #region "Edit"
        public override void DoEdit(DataTable dt)
        {
            ClsUserDAO obj = new ClsUserDAO();
            try
            {
                conn = ClsBaseDbConnectionDAO.GetConnection();
                conn.Open();
                tran = conn.BeginTransaction();

                foreach (DataRow drow in dt.Rows)
                {
                    obj.Edit(drow);
                }

                tran.Commit();
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                {
                    ClsInformation.Information(ex.Message);
                }
                if (tran != null)
                {
                    tran.Rollback();
                }
            }
            finally
            {
                if (conn != null && conn.State != ConnectionState.Closed)
                {
                    conn.Close();
                }
            }
        }

        #endregion

        #region "Delete"
        public override void DoDelete(DataRow drow)
        {
            ClsUserDAO obj = new ClsUserDAO();
            try
            {
                conn = ClsBaseDbConnectionDAO.GetConnection();
                conn.Open();
                tran = conn.BeginTransaction();
                obj.Delete(drow);
                tran.Commit();
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                {
                    ClsInformation.Information(ex.Message);
                }
                if (tran != null)
                {
                    tran.Rollback();
                }
            }
            finally
            {
                if (conn != null && conn.State != ConnectionState.Closed)
                {
                    conn.Close();
                }
            }
        }

        #endregion

        #region GetAllUser
        public override DataTable GetAll()
        {
            ClsUserDAO obj = new ClsUserDAO();
            using (DsUser dsUser = new DsUser())
            {
                dsUser.DtUser.Merge(obj.GetAll());
                dtObject = dsUser.DtUser;
                return dsUser.DtUser;
            }
        }
        #endregion
        //public static bool Login(string strUsername, string strPassword)
        //{
        //    DataRow[] dRow = GetAll().Select(string.Format("UserName='{0}' AND Password='{1}'", strUsername, strPassword));
        //    if (dRow.Length == 0)
        //    {
        //        return false;
        //    }
        //    else
        //    {
        //        return true;
        //    }
        //}
        //#region editPassword
        //public static bool EditPassword(int user_id, string password)
        //{
        //    return true;
        //}
        //#endregion
        //#region getUserByUsername
        ///// <summary>
        ///// getUserByUsername
        ///// </summary>
        ///// <param name="code"></param>
        ///// <returns></returns>
        //public static DataTable getUserByUsername(string username)
        //{
        //    using (DsUser dsUser = new DsUser())
        //    {
        //        DataRow[] drow = GetAllUser().Select(string.Format("UserName={0}", username));
        //        foreach (DataRow row in drow)
        //        {
        //            dsUser.DtUser.Rows.Add(row);
        //        }
        //        return dsUser.DtUser;
        //    }
        //}
        //#endregion
        //#region getUserByCode
        ///// <summary>
        ///// getUserByCode
        ///// </summary>
        ///// <param name="code"></param>
        ///// <returns></returns>
        //public static DataTable getUserByCode(string code)
        //{
        //    using (DsUser dsUser = new DsUser())
        //    {
        //        DataRow[] drow = GetAllUser().Select(string.Format("code={0}", code));
        //        foreach (DataRow row in drow)
        //        {
        //            dsUser.DtUser.Rows.Add(row);
        //        }
        //        return dsUser.DtUser;
        //    }
        //}
        //#endregion
        //#region getTotalUsers
        //public static double getTotalUsers()
        //{
        //    return (double)GetAllUser().Compute("COUNT(*)", "");
        //}
        //#endregion
        //#region getTotalUsersByGroupId
        ///// <summary>
        ///// Lấy tổng số lượng người dùng theo user_group_id
        ///// </summary>
        ///// <param name="user_group_id"></param>
        ///// <returns></returns>
        //public static double getTotalUsersByGroupId(int user_group_id)
        //{
        //    return (double)GetAllUser().Compute("COUNT(*)", string.Format("user_group_id={0}", user_group_id));
        //}
        //#endregion
        //#region getTotalUsersByEmail
        ///// <summary>
        ///// Lấy tổng số lượng user theo email
        ///// </summary>
        ///// <param name="strEmail"></param>
        ///// <returns></returns>
        //public static double getTotalUsersByEmail(string strEmail)
        //{
        //    return (double)GetAllUser().Compute("COUNT(*)", string.Format("email ={0}", strEmail));
        //}
        //#endregion
        //public static DataTable GetByExpresstion(string strExpression)
        //{
        //    using (DsUser dsUser = new DsUser())
        //    {
        //        DataRow[] drow = ClsUserDAO.GetAll().Select(strExpression);
        //        foreach (DataRow row in drow)
        //        {
        //            dsUser.DtUser.Rows.Add(row);
        //        }
        //        return dsUser.DtUser;
        //    }
        //}
    }
}